<?php

class Admin_data extends MY_Controller {
	
	private $errNotLoggedIn = '{ "Result":"ERROR", "Message":"Faça login como administrador" }';
	
	
	function Admin_data() {
		
		parent::__construct();
		
		if ($this->ion_auth->is_admin()) {
			
			$this->load->helpers(array('html','email'));
			
			$this->load->library(array('ion_auth'));
			
			$this->load->model('Admin_model'); 
					
			$this->_tmp();
		}
		else {
			
			exit($this->errNotLoggedIn);
		}
	}
	
	
	
	
	
	private function _tmp() {
		
	}
	
	
	
	
	
	
	
	
	public function jtableCreateUser() {
		
		$ok_msg 			= '{"Result":"OK"}';
		$err_msg 			= '{"Result":"ERROR", "Message":"erro generico" }';
		$err_no_password 	= '{"Result":"ERROR", "Message":"A password nao esta definida" }';
		$err_dont_match 	= '{"Result":"ERROR", "Message":"As passwords nao sao iguais" }';
		$err_invalid_mail 	= '{"Result":"ERROR", "Message":"email nao e valido" }';
		$err_user_exists 	= '{"Result":"ERROR", "Message":"Utilizador ja existe" }';
		$err_not_created 	= '{"Result":"ERROR", "Message":"Não foi possivel criar o utilizador" }';
		
		$group = $_POST['groupName'];
		
		if (isset($_POST['active']))
			$active = true;
		else 
			$active = false;
		
		if (!empty($_POST['email']) && valid_email($_POST['email'])) {
			
			$email = $_POST['email'];
		}
		else {
			
			echo $err_invalid_mail;
		
			return;
		}
		
		if ($this->ion_auth->get_user_by_email($email)) {
			
			echo $err_user_exists;
		
			return;
		}
		
		if (empty($_POST['password']) OR empty($_POST['retypePassword'])) {
			
			echo $err_no_password;
		
			return;
		}
		else {
			
			$password 			= $_POST['password'];
		
			$retypePassword 	= $_POST['retypePassword'];
		}
		
		if ($password != $retypePassword ) {
			
			echo $err_dont_match;
		
			return;
		}
		
		$additional_data = array(
			'first_name' 	=>'', 
			'last_name' 	=>'',
			'company' 		=>''
		);
		
		$id = $this->ion_auth->register($email, $password, $email, $additional_data, $group);
		
		if ($id) {
			
			$fields = array(
				'users.id',
				'users.username',
				'users.active',
				'company', 
				'email',
				'meta.phone',
				'meta.address',
				'groups.name as groupName'
			);
		
			$user = $this->Admin_model->user($fields, $id);
			
			$jTableResult['Result'] = 'OK';
		
			$jTableResult['Record'] = $user;
			
			echo json_encode($jTableResult);
		
			return;
		}
		else {
			
			echo $err_not_created;
			
			return;
		}
	}
	
	public function jtableReadUser() {
		
		$no_records 	= '{"Result":"OK", "Records":[]}';
		$err_generic 	= '{"Result":"ERROR", "Message":"erro generico"}';
		$err_no_data  	= '{"Result":"ERROR", "Message":"Não existem utilizadores"}';
		$this->firephp->log('hello');
		
		$fields = array(
			'users.id',
			'users.username',
			'users.active',
			'company', 
			'email',
			'meta.phone',
			'meta.address',
			'groups.name as groupName'
		);
		
		$users = $this->Admin_model->users($fields);
		
		if (!$users) {
			
			echo $err_no_data;
		
			return;
		}

		$jTableResult['Result'] = 'OK';
		
		$jTableResult['Records'] = $users;
		
		echo json_encode($jTableResult);
		
		return;
	}
	
	public function jtableUpdateUser() {
		
		$ok_msg 				= '{"Result":"OK"}';
		$err_msg 				= '{"Result":"ERROR", "Message":"Não foi possivel actualizar o utilizador" }';
		$err_pass_dont_match 	= '{"Result":"ERROR", "Message":"As passwords nao sao iguais" }';
		
		//$this->firephp->log($_POST);
		
		if(isset($_POST['active']))
			$active = true;
		else
			$active = false;
		
		$email = $_POST['email'];
		
		if ($_POST['password'] != $_POST['retypePassword']) {
			
			echo $err_pass_dont_match;
		
			return;
		}
		
		if (!empty($_POST['password']) && !empty($_POST['retypePassword']) && ($_POST['password'] == $_POST['retypePassword']))
			$this->ion_auth->change_password($email, null, $_POST['password']);
		
		$data = array(
			'active'=>$active
			//'email'	=>$email
		);
		
		//$updated = $this->ion_auth->update_user($_POST['id'], $data);
		$updated = $this->db->update('users', $data,array('id'=>$_POST['id']));
		
		if ($updated) {
			
			echo $ok_msg;
		
			return;
		}
		else {
			
			echo $err_msg;
		
			return;
		}
	}
	
	public function jtableDeleteUser() {
		
		$ok_msg 	= '{"Result":"OK"}';
		$err_msg 	= '{"Result":"ERROR", "Message":"Não foi possivel eliminar o utilizador" }';
		
		$this->firephp->log($_POST);
		
		$deleted = $this->ion_auth->delete_user($_POST['id']);
		
		if ($deleted) {
			
			echo $ok_msg;
		
			return;
		}
		else {
			
			echo $err_msg;
		
			return;
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	public function tmp() {
		
		// 5 -> hotel 1
		//$user = $this->ion_auth->get_user(5);
		$data = array(
			'active'=>false
		);
		
		$this->db->where('id', 5);
		
		$updated = $this->db->update('users', $data);
		
		if ($updated)
			$this->firephp->log('updated');
		else
			$this->firephp->log('not updated');
		
		return;
		//$changed = $this->ion_auth->change_password($user->email,null, '00');
		
		/*
		if ($changed)
			echo 'changed';
		else 
			echo 'not changed';
		*/
		
		//$this->firephp->log($user, 'user: ');
		//$this->ion_auth->change_password($identity, $old, $new);
		
		//$fields = array('users.id','users.username','company', 'email','groups.name as groupName');
		
		//$user = $this->Admin_model->users($fields);
		
		//$this->firephp->log($user);
		
		return;
		
		if ($this->input->is_ajax_request())
			echo 'is ajax request';
		else 
			echo 'not an ajax request';
		
		return;
	}
	
	
}